import { loginApi } from "@/api/login"
import { setToken } from "@/utils/token"
import { setUname } from "@/utils/uname"
import { setRole } from "@/utils/role"
import { setMenu } from "@/utils/menu"

export default {
  state: {
    token: "",
    username: "",
    role: "",
    menu:[]
  },
  mutations: {
    settoken(state, token) {
      setToken(token)
      state.token = token
    },
    setusername(state, name) {
      setUname(name)
      state.username = name
    },
    setrole(state, role) {
      setRole(role)
      state.role = role
    },
    setmenu(state, menu) {
      setMenu(menu)
      state.menu = menu
    }
  },
  actions: {
    async loginFn({ commit }, userdata) {
      let res = await loginApi(userdata)
      // console.log(res);
      if (res.code == 0) {
        commit("settoken", res.token)
        commit("setusername",res.name)
        commit("setrole",res.role) 
        commit("setmenu",res.menu) 
      }
      return res.code == 0 ? true : false
    }
  }
}